import { createRouter, RouteRecordRaw, createWebHistory, RouteLocationNormalizedLoaded } from 'vue-router'; // createWebHashHistory


const activeRouterList = [
    {
      path:'/401',
      component: () => import('@/views/errorPage/401.vue')
    },
    {
      path: '/404',
      component: () => import('@/views/errorPage/404.vue')
    }]

// 关于单层路由，meta 中设置 { single: true } 即可为单层路由，{ hidden: true } 即可在侧边栏隐藏该路由

// 存放动态路由
export const asyncRouterList: Array<RouteRecordRaw> = [...activeRouterList];

// 存放固定的路由
const defaultRouterList = [
  {
    path:'/helloworld',
    name:'HelloWorld',
    component: ()=>import('@/components/HelloWorld.vue')
  },
  {
    path: '/home',
     name: 'Home' ,
    component: () => import('@/views/Home.vue'),
  },
  { path: '/:w+', name: '404Page', component: () => import('@/views/errorPage/404.vue') },
];

export const allRoutes = [...defaultRouterList, ...asyncRouterList];

export const getActive = (maxLevel = 3, route: RouteLocationNormalizedLoaded): string => {
  if (!route?.path) {
    return '';
  }
  return route?.path
    .split('/')
    .filter((_item: string, index: number) => index <= maxLevel && index > 0)
    .map((item: string) => `/${item}`)
    .join('');
};

const router = createRouter({
  history: createWebHistory(), // createWebHistory createWebHashHistory()
  routes: allRoutes,
  scrollBehavior() {
    return {
      el: '#app',
      top: 0,
      behavior: 'smooth',
    };
  },
});

export default router;
